package top.zuli.package01.class14;

import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建线程池 创建一个线程池里面有10个线程
        ExecutorService service = Executors.newFixedThreadPool(10);
        //强转底层实现类
        ThreadPoolExecutor service1 = (ThreadPoolExecutor)service;
        //设置最大线程数
        service1.setMaximumPoolSize(100);
        //调用一个线程执行runnable接口
        service.execute(new Thread1());
        //调用一个线程执行callable接口
        Future future = service.submit(new Thread2());
        Object result = future.get();
        System.out.println(result);
        //关闭线程池
        service.shutdown();

    }
}
class Thread1 implements  Runnable{
    @Override
    public void run() {
        for (int i = 1; i <=100; i++) {
            System.out.println(Thread.currentThread().getName()+"："+i);
        }
    }
}
class Thread2 implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=100 ; i++) {
            System.out.println(Thread.currentThread().getName()+"："+i);
            sum+=i;
        }
        return sum;
    }
}

/*
public class ThreadPoolDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor)service;
        service1.setMaximumPoolSize(100);
        service.execute(new Thread1());
        Future future = service.submit(new Thread2());
        Object result = future.get();
        System.out.println(result);
        service.shutdown();

    }
}
class Thread1 implements  Runnable{
    @Override
    public void run() {
        for (int i = 1; i <=100; i++) {
            System.out.println(Thread.currentThread().getName()+"："+i);
        }
    }
}
class Thread2 implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=100 ; i++) {
            System.out.println(Thread.currentThread().getName()+"："+i);
            sum+=i;
        }
        return sum;
    }
}*/

/*
public class ThreadPoolDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor service1 = (ThreadPoolExecutor)service;
        service1.setMaximumPoolSize(100);
        service.execute(new Thread1());
        Future future = service.submit(new Thread2());
        Object result = future.get();
        System.out.println(result);
        service.shutdown();

    }
}
class Thread1 implements  Runnable{
    @Override
    public void run() {
        for (int i = 1; i <=100; i++) {
            System.out.println(Thread.currentThread().getName()+"："+i);
        }
    }
}
class Thread2 implements Callable {
    @Override
    public Object call() throws Exception {
        int sum = 0;
        for (int i = 1; i <=100 ; i++) {
            System.out.println(Thread.currentThread().getName()+"："+i);
            sum+=i;
        }
        return sum;
    }
}*/
